
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/rb_api.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui/support/support_download_db_file.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/pagestate_ss_setting.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
离线数据下载进度
 */
class PageStateSelfServiceDownloadDBProgress extends XPageState{

  PageStateSelfServiceDownloadDBProgress();

  int progressType=0;

  @override
  Widget getBodyWidget(BuildContext context) {
    return XWidgetUtils.getLayoutCenter(
        XWidgetUtils.getXLayoutRoundedRect(
            height:840,
            radius_topLeft: 30,
            radius_topRight: 30,
            radius_bottomRight: 30,
            radius_bottomLeft: 30,
            margin: XWidgetUtils.getEdgeInsets(50, 0, 50, 0),
            backgroundColor: Colors.white,
            child:XWidgetUtils.getLayoutColumn(
                children: [
                  XWidgetUtils.getLayoutContainer(
                      width: double.infinity,
                      margin: XWidgetUtils.getEdgeInsets(0, 60, 0, 0),
                      child:XWidgetUtils.getLayoutStack(
                          alignment:AlignmentDirectional.center,
                          children: [
                            XWidgetUtils.getWidgetText(
                                '',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 46,
                                    color: XColorUtils.color333333
                                )
                            ),
                            XWidgetUtils.getLayoutPositioned(
                                top: 0,
                                right: 32,
                                child: XWidgetUtils.getWidgetGestureDetector(
                                    child: XImageUtils.getImageWidgetWithAsset(
                                        'ss_close.png',
                                        width: 70,
                                        height: 70
                                    ),
                                    onTap: (){
                                      //关闭
                                      if(progressType>5){
                                        finishPage(result: 'ok');
                                      }else if(progressType==-1){
                                        finishPage();
                                      }else{
                                        XFlutterUtils.openAlertDialogDefault(
                                            context: context,
                                            title: '温馨提示',
                                            contentStr: '正在下载数据，关闭可能导致商品数据或促销信息缺漏。是否关闭？',
                                          onCallbackResult:(bt){
                                              if(bt=='确定'){
                                                finishPage();
                                              }
                                          }
                                        );
                                      }
                                    }
                                )
                            )
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>0?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '数据请求排队中',
                                style: XStyleUtils.getTextStyle(
                                  fontSize: 34,
                                  color: progressType>0?RBColors.reabamColor4:XColorUtils.color333333,
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>1?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '开始拉取商品数据',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: progressType>1?RBColors.reabamColor4:XColorUtils.color333333
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>2?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '正在生成商品数据',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: progressType>2?RBColors.reabamColor4:XColorUtils.color333333
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>3?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '正在下载商品数据',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: progressType>3?RBColors.reabamColor4:XColorUtils.color333333
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>4?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '正在导入本地数据库',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: progressType>4?RBColors.reabamColor4:XColorUtils.color333333
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      padding: XWidgetUtils.getEdgeInsets(80, 22, 80, 0),
                      child:XWidgetUtils.getLayoutRow(
                          children: [
                            progressType>5?XImageUtils.getImageWidgetWithAsset(
                              'ss_gou.png',
                              width: 40,
                              height: 40,
                            ):XWidgetUtils.getLayoutContainer(width: 40,height: 40),
                            XWidgetUtils.getLayoutExpanded(
                              XWidgetUtils.getWidgetText(
                                '数据下载完成',
                                style: XStyleUtils.getTextStyle(
                                    fontSize: 34,
                                    color: progressType>5?RBColors.reabamColor4:XColorUtils.color333333
                                ),
                                padding: XWidgetUtils.getEdgeInsets(20, 0, 0, 0),
                              ),
                            ),
                          ]
                      )
                  ),
                  XWidgetUtils.getLayoutContainer(
                      margin:  XWidgetUtils.getEdgeInsets(80, 100, 80, 0),
                      child: RBWidgets_SelfService.getButton(
                          text: progressType==-1?'重新下载':(progressType>5?'下载完成':'下载中'),
                          backgroundColor:(progressType>5 || progressType==-1)?RBColors.reabamColor4:const Color(0xFFADB1C0),
                          onPressed: () {
                            if(progressType>5){
                              finishPage(result: 'ok');
                            }else if(progressType==-1){
                              updateData();
                            }
                          }
                      )
                  ),
                ]
            )
        )
    );
  }


  @override
  Color getXPageBackgroundColor() {
    return XColorUtils.pageMengcengColor;
  }


  @override
  void onStart() {
  }

  @override
  Future<void> updateData({int pageIndex=1}) async {

    showLoading();
    RBBaseApi.downloadDBFileWaitH5CallbackFileUrl(
        succeed: (dynamic response, Map<String, dynamic> responseMap, Map<String, dynamic> headersResponse) {
          hideLoading();

          SupportDownloadDBFile.downloadOfflineDBFileUrl(responseMap['data'],(res){

            setState(() {
              String code=res['code'];
              int type=res['type'];
              if(code=='-1'){
                progressType=-1;
              }else{
                if(type==1){
                  //开始下载支付方式
                  progressType=1;
                }else if(type==2){
                  //开始下载商品分类
                  progressType=2;
                }else if(type==3){
                  //开始下载离线文件
                  progressType=4;
                }else if(type==4){
                  //开始处理离线文件
                  progressType=5;
                }else if(type==10){
                  //下载完成
                  progressType=10;
                }
              }
            });

          });


          },
        failed: (String code, String msg) {
          hideLoading();
          toast(msg);
          setState(() {
            progressType=-1;
          });
        });

  }




}